package com.example.assign2_list;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;

public class Mp3Service extends Service {
	private ArrayList<String> playList;
	private File sdcard;
	private MediaPlayer mp;
	private IBinder mBinder = new Mp3Binder();
	private boolean firstPlay;
	private int songCount;
	
	@Override
	public void onCreate(){
		playList = new ArrayList<String>();
		sdcard = Environment.getExternalStorageDirectory();
		mp = new MediaPlayer();
		firstPlay = true;
		songCount = 0;
    	
		/*	Start a thread to get list of music */
        Thread listThr = new Thread(null, getList, "mp3List");
        listThr.start();
	}
	
	Runnable getList = new Runnable() {
		
		public void run() {	
	    	File files = new File(sdcard, "/Music/");
	    	File[] mp3List = files.listFiles();

	    	for(int i = 0; i < mp3List.length; i++){
	    		if(mp3List[i].getName().endsWith(".mp3")){
	    			playList.add(mp3List[i].toString());
	    		}
	    	}
		}
	};
	
	public void play(){	
		/*If firstPlay set to first song in list	*/
		if(firstPlay){
			firstPlay = false;
			try {
				mp.setDataSource(playList.get(0));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				mp.prepare();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			mp.start();
		}
		else if(!mp.isPlaying()){
			mp.start();
		}
		
		/*	If song ended, get next song in list! If it is last song get first song in list */
		mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
			
			public void onCompletion(MediaPlayer mp) {
				songCount++;
				mp.stop();
				mp.reset();
				if(songCount < playList.size()){
					try {
						mp.setDataSource(playList.get(songCount));
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (SecurityException e) {
						e.printStackTrace();
					} catch (IllegalStateException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				else{
					songCount = 0;
					try {
						mp.setDataSource(playList.get(songCount));
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (SecurityException e) {
						e.printStackTrace();
					} catch (IllegalStateException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				mp.start();
			}
		});
	}
	
	public void pause(){
		if(mp.isPlaying()){
			mp.pause();
		}
	}
	
	public void next(){
		songCount++;
		if(mp.isPlaying()){
			mp.pause();
			mp.stop();
		}
		mp.reset();
		if(songCount < playList.size()){
			try {
				mp.setDataSource(playList.get(songCount));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else{
			songCount = 0;
			try {
				mp.setDataSource(playList.get(songCount));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			mp.prepare();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		mp.start();
	}
	
	public void prev(){
		songCount--;
		if(mp.isPlaying()){
			mp.pause();
			mp.stop();
		}
		mp.reset();
		if(songCount >= 0){
			try {
				mp.setDataSource(playList.get(songCount));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else{
			songCount = playList.size()-1;
			try {
				mp.setDataSource(playList.get(songCount));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			mp.prepare();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		mp.start();
	}
	
	/*	return sticky because it is a service that's gonna work all the time */
	public int onStartCommand(Intent intent, int flags, int startId){
		return Service.START_STICKY;
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}
	
	
    public class Mp3Binder extends Binder {
    	Mp3Service getService() {
    		return Mp3Service.this;
    	}
    }
}
